package com.atguigu.lease.admin.service.impl;

import com.atguigu.lease.admin.mapper.*;
import com.atguigu.lease.admin.service.*;
import com.atguigu.lease.admin.service.*;
import com.atguigu.lease.model.bean.*;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.atguigu.lease.model.vo.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author LOGIN
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2025-06-21 01:15:32
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService {
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Resource
    RoomFacilityMapper roomFacilityMapper;
    @Resource
    RoomLabelMapper roomLabelMapper;
    @Resource
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Resource
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Resource
    RoomAttrValueMapper roomAttrValueMapper;

    @Override
    public void saveOrUpdateRoomSubmitVo(RoomSubmitVo roomSubmitVo) {
        //todo 1、在执行新增更新房间信息前先判断是更新还是新增
        boolean isUpdate = roomSubmitVo.getId() != null;
        //todo 2、执行房间的新增更新
        this.saveOrUpdate(roomSubmitVo);
        //todo 3、如果是更新，则先删除原来的信息：属性、配套，标签、租期、支付方式、图片
        Long roomId = roomSubmitVo.getId();
        if (isUpdate) {
            //删除 属性
            //delete from room_attr_value where roomId=#{roomId}
            roomAttrValueService.remove(Wrappers.lambdaQuery(RoomAttrValue.class)
                    .eq(RoomAttrValue::getRoomId, roomId));
            //删除 配套
            //delete from room_facility where roomId=#{roomId}
            roomFacilityService.remove(Wrappers.lambdaQuery(RoomFacility.class)
                    .eq(RoomFacility::getRoomId, roomId));
            //删除 标签
            roomLabelService.remove(Wrappers.lambdaQuery(RoomLabel.class)
                    .eq(RoomLabel::getRoomId, roomId));
            //删除 租期
            roomLeaseTermService.remove(Wrappers.lambdaQuery(RoomLeaseTerm.class)
                    .eq(RoomLeaseTerm::getRoomId, roomId));
            //删除 支付方式
            roomPaymentTypeService.remove(Wrappers.lambdaQuery(RoomPaymentType.class)
                    .eq(RoomPaymentType::getRoomId, roomId));
            //删除图片
            graphInfoService.remove(Wrappers.lambdaQuery(GraphInfo.class)
                    .eq(GraphInfo::getItemType, ItemTypeEnum.APARTMENT)
                    .eq(GraphInfo::getItemId, roomId));
        }
        //todo 4、然后再保存属性、配套，标签、租期、支付方式、图片等数据
        //保存 属性
        List<RoomAttrValue> roomAttrValues = roomSubmitVo.getAttrValueIds().stream()
                .map(attrValueId -> {
                    RoomAttrValue roomAttrValue = new RoomAttrValue();
                    roomAttrValue.setAttrValueId(attrValueId);
                    roomAttrValue.setRoomId(roomId);
                    return roomAttrValue;
                }).collect(Collectors.toList());
        roomAttrValueService.saveBatch(roomAttrValues);
        //保存配套
        List<RoomFacility> roomFacilities = roomSubmitVo.getFacilityInfoIds().stream()
                .map(facilityInfoIds -> {
                    RoomFacility roomFacility = new RoomFacility();
                    roomFacility.setRoomId(roomId);
                    roomFacility.setFacilityId(facilityInfoIds);
                    return roomFacility;
                }).collect(Collectors.toList());
        roomFacilityService.saveBatch(roomFacilities);
        //保存标签
        List<RoomLabel> roomLabels = roomSubmitVo.getLabelInfoIds().stream()
                .map(labelId -> {
                    RoomLabel roomLabel = new RoomLabel();
                    roomLabel.setRoomId(roomId);
                    roomLabel.setLabelId(labelId);
                    return roomLabel;
                }).collect(Collectors.toList());
        //批量保存
        roomLabelService.saveBatch(roomLabels);

        //租期
        List<RoomLeaseTerm> roomLeaseTerms = roomSubmitVo.getLeaseTermIds().stream()
                .map(leaseTermId -> {
                    //新建roomLeaseTerm实体对象
                    RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                    //设置roomLeaseTerm实体对象的房间id
                    roomLeaseTerm.setRoomId(roomId);
                    //设置实体对象属性值Id
                    roomLeaseTerm.setLeaseTermId(leaseTermId);
                    return roomLeaseTerm;
                }).collect(Collectors.toList());
        //批量保存
        roomLeaseTermService.saveBatch(roomLeaseTerms);

        //支付方式
        List<RoomPaymentType> roomPaymentTypes = roomSubmitVo.getPaymentTypeIds().stream()
                .map(paymentId -> {
                    RoomPaymentType roomPaymentType = new RoomPaymentType();
                    roomPaymentType.setRoomId(roomId);
                    roomPaymentType.setPaymentTypeId(paymentId);
                    return roomPaymentType;
                }).collect(Collectors.toList());
        roomPaymentTypeService.saveBatch(roomPaymentTypes);
        //图片
        List<GraphInfo> graphInfos = roomSubmitVo.getGraphVoList().stream()
                .map(graphVo -> {
                    GraphInfo graphInfo = new GraphInfo();
                    BeanUtils.copyProperties(graphVo, graphInfo);
                    graphInfo.setItemId(roomId);
                    graphInfo.setItemType(ItemTypeEnum.ROOM.getCode());
                    return graphInfo;
                }).collect(Collectors.toList());
        graphInfoService.saveBatch(graphInfos);

    }

    @Override
    public Page<RoomItemVo> getPageItem(Long current, Long size, RoomQueryVo roomQueryVo) {
        return baseMapper.selectRoomItemVoPage(new Page<>(current,size),roomQueryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1、房间数据
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //2、配套
        roomDetailVo.setFacilityInfoList(roomFacilityMapper.selectFacilityInfosByRoomId(id));
        //3、标签数据
        roomDetailVo.setLabelInfoList(roomLabelMapper.selectLabelInfosByRoomId(id));
        //4、支付方式
        roomDetailVo.setPaymentTypeList(roomPaymentTypeMapper.selectPaymentListByRoomId(id));
        //5、 租期
        roomDetailVo.setLeaseTermList(roomLeaseTermMapper.selectLeaseTermListById(id));
        //6、 属性值
        roomDetailVo.setAttrValueVoList(roomAttrValueMapper.selectAttrValueVoListById(id));
        //7、 公寓
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);
        //8、 图片
        roomDetailVo.setGraphVoList(
                //到数据库中查询GraphInfo集合:房间的图片列表
                graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,ItemTypeEnum.ROOM.getCode()))
                 //  将GraphInfo列表转为 GraphVo集合
                .stream()
                .map(graphInfo->{
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo,graphVo);
                    return graphVo;
                }).collect(Collectors.toList())
                );

        return roomDetailVo;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteRoomById(Long id) {
        //删除房间信息
        this.removeById(id);
        //删除配套
        roomFacilityService.remove(Wrappers.lambdaQuery(RoomFacility.class)
                .eq(RoomFacility::getRoomId,id));
        //3、删除标签数据
        roomLabelService.remove(Wrappers.lambdaQuery(RoomLabel.class)
                .eq(RoomLabel::getRoomId,id));
        //4、删除支付方式
        roomPaymentTypeService.remove(Wrappers.lambdaQuery(RoomPaymentType.class)
                .eq(RoomPaymentType::getRoomId,id));
        //5、 删除租期
        roomLeaseTermService.remove(Wrappers.lambdaQuery(RoomLeaseTerm.class)
                .eq(RoomLeaseTerm::getRoomId,id));
        //6、 删除属性值
        roomAttrValueService.remove(Wrappers.lambdaQuery(RoomAttrValue.class)
                .eq(RoomAttrValue::getRoomId,id));
        //7、 删除图片
        graphInfoService.remove(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,ItemTypeEnum.ROOM));
    }

    @Override
    public List<RoomInfo> listBasicByApartmentId(Long id) {

        return baseMapper.selectBasicByApartmentId(id);
    }

}



